SpringBoot 2.0 实现自定义接口参数解析器

序号:02

对于前端作为参数传过来的json数据,Spring是如何转换为Java Bean的, 又如何自定义这一过程呢?

Spring将参数中的json转为Java Bean主要依赖于@RequestBody注解,该注解的作用原理,请看:https://www.jianshu.com/p/c1b8315c5a03

下面讲如何实现自定义这一过程:

1、首先,自定义一个注解,使用该注解标记的参数则使用自定义的参数解析器

MyRequestBody.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.example.springboottest.annotation;

import java.lang.annotation.*;

/**
* <p> 自定义参数解析注解 <p>
*
* @author: chen
* @create: 2021-04-08
**/
@Documented
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyRequestBody {

}

2、实现自定义参数解析器

CustomJsonMethodArgumentResolver.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.example.springboottest.config;

import com.example.springboottest.annotation.MyRequestBody;
import com.google.gson.Gson;
import org.apache.commons.io.IOUtils;
import org.springframework.core.MethodParameter;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.nio.charset.Charset;

/**
* <p> <p>
*
* @author: chen
* @create: 2021-04-08
**/
public class CustomJsonMethodArgumentResolver implements HandlerMethodArgumentResolver {
@Override
public boolean supportsParameter(MethodParameter parameter) {
//被自定义注解标记的参数使用该解析器
if(parameter.hasParameterAnnotation(MyRequestBody.class)){
return true;
}
return false;
}

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
String body = getRequestBody(webRequest);
if(StringUtils.isEmpty(body)){
return null;
}
System.out.println(body);
//TODO 可以在这里实现一些自定操作,例如自定json的解析过程
return new Gson().fromJson(body,parameter.getParameterType());
}

private String getRequestBody(NativeWebRequest webRequest) {
HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
try {

return IOUtils.toString(servletRequest.getInputStream(), Charset.forName("utf-8"));
}catch (Exception e){
e.printStackTrace();
}
return "";
}
}

3、注入自定义解析器

CustomWebMvcConfigurer.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.example.springboottest.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
* <p> <p>
*
* @author: chen
* @create: 2021-04-08
**/
@Configuration
public class CustomWebMvcConfigurer implements WebMvcConfigurer {

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(new CustomJsonMethodArgumentResolver());
}
}

4、测试接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.example.springboottest.controller;

import com.example.springboottest.annotation.MyRequestBody;
import com.example.springboottest.entity.Person;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
* <p> <p>
*
* @author: chen
* @create: 2021-04-08
**/
@ResponseBody
@Controller
@RequestMapping("/test")
public class TestController {

@PostMapping(value = "/post")
public String testApi(@MyRequestBody Person person){
return person.getName();
}
}

5、效果图

SpringBoot 2.0 实现自定义接口参数解析器

http://leofitz1024.github.io/2020/04/07/Spring Boot/02/

作者

xchen

发布于

2020-04-07

更新于

2021-11-22

评论